//
//  UILabel+ChangeLineSpaceAndWordSpace.m
//  JiHe_IOS
//
//  Created by GuoliWang on 2018/9/3.
//  Copyright © 2018年 JiHe. All rights reserved.
//

#import "UILabel+ChangeLineSpaceAndWordSpace.h"

@implementation UILabel (ChangeLineSpaceAndWordSpace)

- (void)changeLineSpace:(float)space {
    
    NSString *labelText = self.text;
    
    if (stringIsEmpty(labelText)) {
        return;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    paragraphStyle.alignment = self.textAlignment;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    //  [self sizeToFit];
    
}

- (void)changeWordSpace:(float)space {
    
    NSString *labelText = self.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    //  [self sizeToFit];
    
}

- (void)changeSpace:(float)lineSpace WordSpace:(float)wordSpace {
    
    NSString *labelText = self.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    paragraphStyle.alignment = self.textAlignment;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    //  [self sizeToFit];
    
}


#pragma mark ----------------    改变富文本间距      ------------------
- (void)changeLineSpace:(float)space att:(NSMutableAttributedString *)attributedString{
    NSString *labelText = self.text;
    if (!attributedString) {
        if (labelText.length == 0) {
            return;
        }
        attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    }
    
    if ([attributedString isKindOfClass:[NSAttributedString class]]) {//不可变
        attributedString = [[NSMutableAttributedString alloc]initWithAttributedString:attributedString];
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    paragraphStyle.alignment = self.textAlignment;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    //  [self sizeToFit];
    
}

- (void)changeAttWordSpace:(float)space att:(NSMutableAttributedString *)attributedString{
    
    NSString *labelText = self.text;
    
    if (!attributedString) {
        attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    //  [self sizeToFit];
    
}

- (void)changeSpace:(float)lineSpace WordSpace:(float)wordSpace att:(NSMutableAttributedString *)attributedString{
    
    NSString *labelText = self.text;
    if (!attributedString) {
        attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    paragraphStyle.alignment = self.textAlignment;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    //  [self sizeToFit];
    
}

/**
 给uilabel字设置阴影
 
 @param shadowColor 阴影颜色
 @param shadowBlurRadius 半径
 @param shadowOffset 偏移
 */
- (void)setTextShadow:(UIColor *)shadowColor shadowBlurRadius:(CGFloat)shadowBlurRadius shadowOffset:(CGSize)shadowOffset text:(NSString *)text{
    NSShadow * shadow = [[NSShadow alloc]init];
    shadow.shadowBlurRadius = shadowBlurRadius;
    shadow.shadowOffset = shadowOffset;
    shadow.shadowColor = shadowColor;
    
    NSMutableAttributedString * att = [[NSMutableAttributedString alloc]initWithString:text attributes:@{NSShadowAttributeName:shadow}];
    
    self.attributedText = att;
}

@end
